/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package l1j.server.server.model.item.action;

import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.identity.L1ItemId;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.serverpackets.S_Liquor;
import l1j.server.server.serverpackets.S_SkillBrave;
import l1j.server.server.serverpackets.S_SkillSound;

import static l1j.server.server.model.skill.L1SkillId.*;

public class Potion {
	/** 2段加速效果 **/
	public static void Brave(L1PcInstance pc, L1ItemInstance item, int item_id) {
		if (pc.getSkillEffectTimerSet().hasSkillEffect(DECAY_POTION)) { // 药水霜化术状态
			pc.sendPackets(new S_ServerMessage(698)); // 喉咙灼热，无法喝东西。
			return;
		}

		cancelAbsoluteBarrier(pc);

		int time = 0;

		// 判断持续时间 && 使用类型
		/* 勇敢药水类 */
		if (item_id == L1ItemId.POTION_OF_EMOTION_BRAVERY
				|| item_id == L1ItemId.B_POTION_OF_EMOTION_BRAVERY
				|| item_id == L1ItemId.POTION_OF_REINFORCED_CASE
				|| item_id == L1ItemId.W_POTION_OF_EMOTION_BRAVERY
				|| item_id == L1ItemId.DEVILS_BLOOD
				|| item_id == L1ItemId.COIN_OF_REPUTATION) {
			if (item_id == L1ItemId.POTION_OF_EMOTION_BRAVERY) { // 勇敢药水
				time = 300;
			} else if (item_id == L1ItemId.B_POTION_OF_EMOTION_BRAVERY) { // 受祝福的勇敢药水
				time = 350;
			} else if (item_id == L1ItemId.POTION_OF_REINFORCED_CASE) { // 强化勇气的药水
				time = 1800;
			} else if (item_id == L1ItemId.DEVILS_BLOOD) { // 恶魔之血
				time = 600;
			} else if (item_id == L1ItemId.COIN_OF_REPUTATION) { // 名誉货币
				time = 600;
			} else if (item_id == L1ItemId.W_POTION_OF_EMOTION_BRAVERY) { // 福利勇敢药水
				time = 1200;
			}
			buff_brave(pc, STATUS_BRAVE, (byte) 1, time); // 给予勇敢药水效果
			pc.getInventory().removeItem(item, 1);
			/* 精灵饼干 & 祝福的精灵饼干 */
		} else if (item_id == L1ItemId.ELVEN_WAFER
				|| item_id == L1ItemId.B_ELVEN_WAFER
				|| item_id == L1ItemId.W_POTION_OF_FOREST) {
			if (item_id == L1ItemId.ELVEN_WAFER) { // 精灵饼干
				time = 600;
			} else if (item_id == L1ItemId.B_ELVEN_WAFER) { // 祝福的精灵饼干
				time = 700;
			} else if (item_id == L1ItemId.W_POTION_OF_FOREST) { // 福利森林药水
				time = 1920;
			}
			buff_brave(pc, STATUS_ELFBRAVE, (byte) 3, time); // 给予精灵饼干效果
			pc.getInventory().removeItem(item, 1);
			/* 生命之树果实 */
		} else if (item_id == L1ItemId.FORBIDDEN_FRUIT) { // 生命之树果实
			time = 480;
			pc.getSkillEffectTimerSet().setSkillEffect(STATUS_RIBRAVE, time * 1000);
			pc.sendPackets(new S_SkillSound(pc.getId(), 7110));
			pc.broadcastPacket(new S_SkillSound(pc.getId(), 7110));
			pc.getInventory().removeItem(item, 1);
		}
	}

	private static void buff_brave(L1PcInstance pc, int skillId, byte type,
			int timeMillis) {
		// 消除重复状态
		if (pc.getSkillEffectTimerSet().hasSkillEffect(STATUS_BRAVE)) { // 勇敢药水类 1.33倍
			pc.getSkillEffectTimerSet().killSkillEffectTimer(STATUS_BRAVE);
		}
		if (pc.getSkillEffectTimerSet().hasSkillEffect(STATUS_ELFBRAVE)) { // 精灵饼干 1.15倍
			pc.getSkillEffectTimerSet().killSkillEffectTimer(STATUS_ELFBRAVE);
		}
		if (pc.getSkillEffectTimerSet().hasSkillEffect(HOLY_WALK)) { // 神圣疾走 移速1.33倍
			pc.getSkillEffectTimerSet().killSkillEffectTimer(HOLY_WALK);
		}
		if (pc.getSkillEffectTimerSet().hasSkillEffect(MOVING_ACCELERATION)) { // 行走加速 移速1.33倍
			pc.getSkillEffectTimerSet().killSkillEffectTimer(MOVING_ACCELERATION);
		}
		if (pc.getSkillEffectTimerSet().hasSkillEffect(WIND_WALK)) { // 风之疾走 移速1.33倍
			pc.getSkillEffectTimerSet().killSkillEffectTimer(WIND_WALK);
		}
		if (pc.getSkillEffectTimerSet().hasSkillEffect(BLOODLUST)) { // 血之渴望 攻速1.33倍
			pc.getSkillEffectTimerSet().killSkillEffectTimer(BLOODLUST);
		}
		if (pc.getSkillEffectTimerSet().hasSkillEffect(STATUS_BRAVE2)) { // 超级加速 2.66倍
			pc.getSkillEffectTimerSet().killSkillEffectTimer(STATUS_BRAVE2);
		}
		// 给予状态 && 效果
		pc.getSkillEffectTimerSet().setSkillEffect(skillId, timeMillis * 1000);
		pc.sendPackets(new S_SkillSound(pc.getId(), 751));
		pc.broadcastPacket(new S_SkillSound(pc.getId(), 751));
		pc.sendPackets(new S_SkillBrave(pc.getId(), type, timeMillis));
		pc.broadcastPacket(new S_SkillBrave(pc.getId(), type, 0));
		pc.setBraveSpeed(type);
	}

	/** 2段加速效果 结束 **/

	/** 3段加速效果 **/
	public static void ThirdSpeed(L1PcInstance pc, L1ItemInstance item, int time) {
		if (pc.getSkillEffectTimerSet().hasSkillEffect(DECAY_POTION)) { // 药水霜化术状态
			pc.sendPackets(new S_ServerMessage(698)); // 喉咙灼热，无法喝东西。
			return;
		}

		cancelAbsoluteBarrier(pc);

		if (pc.getSkillEffectTimerSet().hasSkillEffect(EFFECT_THIRD_SPEED)) {
			pc.getSkillEffectTimerSet().killSkillEffectTimer(EFFECT_THIRD_SPEED);
		}

		pc.getSkillEffectTimerSet().setSkillEffect(EFFECT_THIRD_SPEED, time * 1000);

		pc.sendPackets(new S_SkillSound(pc.getId(), 8031));
		pc.broadcastPacket(new S_SkillSound(pc.getId(), 8031));
		pc.sendPackets(new S_Liquor(pc.getId(), 8)); // 人物 * 1.15
		pc.broadcastPacket(new S_Liquor(pc.getId(), 8)); // 人物 * 1.15
		pc.sendPackets(new S_ServerMessage(1065)); // 将发生神秘的奇迹力量。
		pc.getInventory().removeItem(item, 1);
	}

	/** 3段加速效果 结束 **/

	private static void cancelAbsoluteBarrier(L1PcInstance pc) { // 绝对屏障效果解除
		if (pc.getSkillEffectTimerSet().hasSkillEffect(ABSOLUTE_BARRIER)) {
			pc.getSkillEffectTimerSet().killSkillEffectTimer(ABSOLUTE_BARRIER);
			pc.startHpRegeneration();
			pc.startMpRegeneration();
			pc.startMpRegenerationByDoll();
		}
	}
}
